/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.zosean.sky.stringItem;

import java.io.DataInputStream;
import java.io.UnsupportedEncodingException;
import java.util.Hashtable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author Administrator
 */
class ReadTextsFile
{

    /**
     * Default construction method
     * 默认构造
     */
    public ReadTextsFile()
    {
    }

    /**
     * Read the texts file 
     * @param fileName The locale file's name 
     * @return reutrn string of the locale file 
     */
    public final String readFile(String fileName)
    {
        if (getClass().getResourceAsStream(fileName) == null)
        {
            return null;
        }
        DataInputStream dis = new DataInputStream(getClass().getResourceAsStream(fileName));
        int initLength = 512;
        int expansionLength = 512;
        byte[] returnByteArray = new byte[expansionLength];
        byte[] tempStorByteArray;
        String return_String = null;
        try
        {
            int ch; // 每次读出来的字符
            int currentIndex = 0;
            while ((ch = dis.read()) != -1)
            {
                // Dealing with a single string
//                if(ch == ' ')
//                    continue;
                returnByteArray[currentIndex] = (byte) ch;
                currentIndex++;
                if (currentIndex >= returnByteArray.length)
                {
                    initLength += expansionLength;
                    tempStorByteArray = new byte[initLength];
                    System.arraycopy(returnByteArray, 0, tempStorByteArray, 0, currentIndex);
                    returnByteArray = null;
                    returnByteArray = tempStorByteArray;
                }
            }
            if (initLength % expansionLength == 0)
            {
                tempStorByteArray = new byte[currentIndex];
                System.arraycopy(returnByteArray, 0, tempStorByteArray, 0, currentIndex);
                returnByteArray = null;
                returnByteArray = tempStorByteArray;
            }
            return_String = new String(returnByteArray, "UTF-8");
            if (dis != null)
            {
                dis.close();
                dis = null;
            }
        } catch (Exception e)
        {
            System.out.println("READ FILE ERROR :" + e.toString());
        }
        return return_String;
    }

    /**
     * Read the font image
     * @param imageFileName  The image file name
     * @return return a picture
     */
    public Image readImageFile(String imageFileName)
    {
        if (imageFileName == null || imageFileName.equals(""))
        {
            System.out.println(">> WRANING :" + "The image file is empty");
            return null;
        }
        String tempImageFileNameHead = "/";
        String tempImageFileNameEnd = ".png";
        StringBuffer sb = new StringBuffer();
        Image returnImage = null;
        sb.append(tempImageFileNameHead);
        sb.append(imageFileName);
        sb.append(tempImageFileNameEnd);
        try
        {
            if (returnImage == null)
            {
                returnImage = Image.createImage(sb.toString());
            }
        } catch (Exception ex)
        {
            ex.printStackTrace();
        }
        return returnImage;
    }
    /**
     * Cutting the resouce image to samll image
     * @param imageValue
     * @param smallImageWidth
     * @param smallImageHeight
     * @return
     */
    public final Image[] cutImage(Image imageValue, int smallImageWidth, int smallImageHeight)
    {
        if(imageValue == null)
        {
            System.out.println(">>WRANING : " + "The resouce image is empty");
            return null;
        }
        if(smallImageWidth  <= 0 || smallImageHeight <= 0)
        {
            System.out.println(">>WARNING : " + "The small picture width or small picture height is empty");
            return null;
        }
        int tempRow = imageValue.getWidth() / smallImageWidth; // x 轴格子数
        int tempCol = imageValue.getHeight() / smallImageHeight;
        int titleNumber = tempRow * tempCol;
        Image []  returnImageArray = new Image[titleNumber];
        for(int i = 0; i < titleNumber; i++)
        {
            int cx = i % tempRow * smallImageWidth;
            int cy = i / tempRow * smallImageHeight;
            returnImageArray[i] = Image.createImage(imageValue, cx, cy, smallImageWidth, smallImageHeight, 0);
        }
        return returnImageArray;
    }

    /**
     * Text and picture are strored in the hash table
     * @param strValue    Key string
     * @param imageArray  Value image
     * @return Returns a hash table
     */
    public final Hashtable setStringToImage(String strValue, Image [] imageArray)
    {
//        System.out.println(strValue + "||"  + imageArray + "||" );
        if(strValue == null || imageArray == null || imageArray.length < 0)
        {
            System.out.println(">>WARNING : " + "The set string to image ERROR");
            return null;
        }
        Hashtable ht = new Hashtable();
        int minLength;
        if(strValue.length() != imageArray.length)
        {
            System.out.println(">>WARNING : " + "The string and picture not the same as length of the array");
            minLength = Math.min(strValue.length(), imageArray.length);
        }else
            minLength = imageArray.length;
        for(int i = 0; i < minLength; i++)
        {
            ht.put(String.valueOf(strValue.charAt(i)), imageArray[i]);
        }
        return ht;
    }
}
